using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Contact_Manager
{
    public partial class Import : Form
    {
        public Import()
        {
            InitializeComponent();
        }
        
        private void Import_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Import_KeyDown);
        }

        private void Import_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape) this.Close();
            else if (e.KeyData == Keys.Enter) this.ImportData();
        }

        private void ImportData()
        {
            string s = "";
            if (radClip.Checked)
            {
                s = Clipboard.GetText();
            }
            else
            {
                try
                {
                    StreamReader fr = new StreamReader(this.txtFile.Text);
                    s = fr.ReadToEnd();
                    fr.Close();
                }
                catch { }
            }
            if (s.Trim()!="") s += Environment.NewLine + Environment.NewLine;
            ((ContactManager)this.Owner).AddRtbText(s);
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.ImportData();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            if (fileDialog.ShowDialog(this) == DialogResult.OK)
            {
                txtFile.Text = fileDialog.FileName;
            }
        }

        private void radFile_CheckedChanged(object sender, EventArgs e)
        {
            this.txtFile.Enabled = radFile.Checked;
            this.btnBrowse.Enabled = radFile.Checked;
        }
    }
}